import store from '@/codecat/store'
import dayjs from 'dayjs'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isSameOrBefore)
dayjs.extend(isBetween)

//判断是否在微信中
export const isWechat = () => {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/micromessenger/i) == "micromessenger") {
		return true;
	} else {
		return false;
	}
}
/**
 * 图片资源的转换-本地图片
 */
export const getImage = (val) => {
	if (val) {
		if (val.indexOf('http') === 0 || val.indexOf('data:image/') !== -1) {
			return val
		} else if (val.indexOf('.') !== -1) {
			return `/static/image/${val}`
		} else {
			return `/static/image/${val}.png`
		}
	} else {
		return ''
	}
}

/**
 * 图片资源的转换-线上图片
 */
export const getSourceUrl = (val) => {
	if (val && store.getters.sysConfig) {
		const {
			cdnurl
		} = store.getters.sysConfig
		return `${cdnurl}${val}`
	} else {
		return ''
	}
}

/**
 * 注:新增选项以追加方式,有些页面默认获取第一个
 */
export const shortcuts = () => {
	const today = dayjs().format('YYYY-MM-DD 23:59:59')
	return [{
		label: '近一周',
		value: () => {
			const start = dayjs().subtract(1, 'w').format('YYYY-MM-DD 00:00:00')
			return [start, today]
		}
	}, {
		label: '近一月',
		value: () => {
			const start = dayjs().subtract(1, 'M').format('YYYY-MM-DD 00:00:00')
			return [start, today]
		}
	}, {
		label: '近三月',
		value: () => {
			const start = dayjs().subtract(3, 'M').format('YYYY-MM-DD 00:00:00')
			return [start, today]
		}
	}, {
		label: '今日',
		value: () => {
			const start = dayjs().startOf('d').format('YYYY-MM-DD HH:mm:ss')
			const end = dayjs().endOf('d').format('YYYY-MM-DD HH:mm:ss')
			return [start, end]
		}
	}, {
		label: '今周',
		value: () => {
			const start = dayjs().startOf('w').format('YYYY-MM-DD HH:mm:ss')
			const end = dayjs().endOf('w').format('YYYY-MM-DD HH:mm:ss')
			return [start, end]
		}
	}, {
		label: '今月',
		value: () => {
			const start = dayjs().startOf('M').format('YYYY-MM-DD HH:mm:ss')
			const end = dayjs().endOf('M').format('YYYY-MM-DD HH:mm:ss')
			return [start, end]
		}
	}]
}

export const copy = (data) => {
	if (!data) return
	uni.setClipboardData({
		data,
		success: () => {
			uni.showToast({
				icon: 'none',
				title: '复制成功'
			})
		}
	});
}

export const phoneCall = (phoneNumber) => {
	if (!phoneNumber) return
	if (!uni.$u.test.mobile(phoneNumber)) {
		uni.showToast({
			icon: 'none',
			title: '手机号码不正确'
		})
		return false
	}
	uni.makePhoneCall({
		phoneNumber,
		fail(err) {
			console.log('makePhoneCall', err.errMsg || 'makePhoneCall fail');
		}
	});
}

export const buildDateRange = (startDate, endDate) => {
	if (!startDate || !endDate) return
	const dateArray = [];
	let currentDate = startDate;
	while (dayjs(currentDate).isBefore(endDate)) {
		dateArray.push(currentDate);
		currentDate = dayjs(currentDate).add(1, 'day').format('YYYY-MM-DD');
	}
	dateArray.push(endDate)
	return dateArray
}

export const getCurrentWeek = (format = 'YYYY-MM-DD') => {
	const startDate = dayjs().startOf('w').format(format)
	const endDate = dayjs().endOf('w').format(format)
	return [startDate, endDate]
}

/**
 * 获取当前页面具体路径，包含参数
 * 用于登录后返回当前页
 */
export function getCurrentPageUrl() {
	let url = ''
	const pages = getCurrentPages()
	const perpage = pages[pages.length - 1] //当前页面
	const keyList = Object.keys(perpage.options) //当前页面携带的路由参数

	if (keyList.length > 0) {
		let keys = '?'
		keyList.forEach((item, index) => {
			index === 0 ?
				keys = keys + item + '=' + perpage.options[item] : keys = keys + '&' + item + '=' + perpage
				.options[item]
		})
		url = '/' + perpage.route + keys
	} else {
		url = '/' + perpage.route
	}
	return url
}

/**
 * 联系方式如下
 * QQ: 535671113
 * 微信: malicat0409
 */
export function onBlock() {
	uni.showToast({
		icon: 'none',
		title: '部分接口暂未开放,联系作者获取完整版!',
		duration: 5000
	})
}